<?php
require_once 'pictureDTO.php';
require_once 'dbHelper.php';

class pictureDAO extends dbHelper
{
    public function create($pictureDTO)
	{
		$sql = "insert into picture(title, postDate, location, description, rate, typeId, accId, albId)
                values('$pictureDTO->title',
                        '$pictureDTO->postDate',
                        '$pictureDTO->location',
                        '$pictureDTO->description',
                        '$pictureDTO->rate',
                        '$pictureDTO->typeId',
                        '$pictureDTO->accId',
                        '$pictureDTO->albId')";
		parent::execQuery($sql);
		if(parent::getAffectedRows() > 0)
			return true;
		return false;
	}
    
    public function update($pictureDTO)
	{
		$sql = "update picture set title =  '$pictureDTO->title',
                                            '$pictureDTO->location',
                                            '$pictureDTO->description',
                                            '$pictureDTO->rate',
                                            '$pictureDTO->typeId',
                                            '$pictureDTO->albId'";
		parent::execQuery($sql);
		if(parent::getAffectedRows() > 0)
			return true;
		return false;
	}
    
    public function delete($picId)
	{
		$sql = "delete from picture where picId = '$picId'";
		parent::execQuery($sql);
		if(parent::getAffectedRows() > 0)
			return true;
		return false;
	}
    
    public function selectAll()
	{
		$list = array();
		$sql = "select * from picture";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$picDTO = new pictureDTO($row[picId], $row[title],
                                         $row[postDate], $row[location],
                                         $row[url], $row[description],
                                         $row[rate], $row[typeId],
                                         $row[accId], $row[albId]);
				$list[count($list)] = $picDTO;
			}
		}
		return $list;
	}
    
    public function selecttop14_orderbypostdate()
	{
		$list = array();
		$sql = "select * from picture order by postDate DESC limit 14";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$picDTO = new pictureDTO($row[picId], $row[title],
                                         $row[postDate], $row[location],
                                         $row[url], $row[description],
                                         $row[rate], $row[typeId],
                                         $row[accId], $row[albId]);
				$list[count($list)] = $picDTO;
			}
		}
		return $list;
	}
    
    public function selecttop14_orderbyrate()
	{
		$list = array();
		$sql = "select * from picture order by rate DESC limit 14";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$picDTO = new pictureDTO($row[picId], $row[title],
                                         $row[postDate], $row[location],
                                         $row[url], $row[description],
                                         $row[rate], $row[typeId],
                                         $row[accId], $row[albId]);
				$list[count($list)] = $picDTO;
			}
		}
		return $list;
	}
    
    public function selecttop14_bytypeid_orderbypostdate($typeId)
	{
		$list = array();
		$sql = "select * from picture where typeId = '$typeId' order by postDate DESC limit 14";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$picDTO = new pictureDTO($row[picId], $row[title],
                                         $row[postDate], $row[location],
                                         $row[url], $row[description],
                                         $row[rate], $row[typeId],
                                         $row[accId], $row[albId]);
				$list[count($list)] = $picDTO;
			}
		}
		return $list;
	}
    
    public function selecttop14_bytypeid_orderbyrate($typeId)
	{
		$list = array();
		$sql = "select * from picture where typeId = '$typeId' order by rate DESC limit 14";
		$result = parent::execQuery($sql);
		if($result && parent::getNumRows($result) > 0)
		{
			while($row = parent::fetchArray($result))
			{
				$picDTO = new pictureDTO($row[picId], $row[title],
                                         $row[postDate], $row[location],
                                         $row[url], $row[description],
                                         $row[rate], $row[typeId],
                                         $row[accId], $row[albId]);
				$list[count($list)] = $picDTO;
			}
		}
		return $list;
	}
}
?>